package org.mclll.xwangplugin.town.command.executor;

import com.google.common.collect.Lists;
import de.tr7zw.nbtapi.NBT;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.apache.commons.lang.StringUtils;
import org.black_ixx.bossshop.BossShop;
import org.black_ixx.bossshop.core.BSBuy;
import org.black_ixx.bossshop.core.BSShop;
import org.black_ixx.bossshop.core.prices.BSPriceType;
import org.black_ixx.bossshop.core.rewards.BSRewardType;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.mclll.xwangplugin.town.database.domain.TownDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;
import org.mclll.xwangplugin.town.enums.TownLevel;
import org.mclll.xwangplugin.util.PlayerTools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.mclll.xwangplugin.town.config.TownContext.TOWN_TP_COMMAND_FORMAT;

public class TownWarpExecutor {
    private static final String menuName = "townTp";
    private static final String tpCommand = "tppos %s %s";
    public static void tp(CommandSender sender, String[] args) {
        Player player = null;
        if (args.length == 2) {
            OfflinePlayer offlineOrOnlinePlayerByName = PlayerTools.getOfflineOrOnlinePlayerByName(args[1]);
            if (!offlineOrOnlinePlayerByName.isOnline()) {
                player.sendMessage("目标玩家不在线");
                return;
            }
            player = (Player) offlineOrOnlinePlayerByName;
        } else if (args.length != 1){
            player.sendMessage(TOWN_TP_COMMAND_FORMAT);
            return;
        }
        if (player == null) {
            if (sender instanceof Player) {
                player = (Player) sender;
            } else {
                player.sendMessage(TOWN_TP_COMMAND_FORMAT);
                return;
            }
        }
        BossShop bs = (BossShop) Bukkit.getPluginManager().getPlugin("BossShopPro");
        if (bs == null) {
            return;
        }
        BSShop shop = bs.getAPI().getShop(menuName);
        shop.getItems().clear();
        List<TownDO> townList = TownManage.selectList(null);
        for (int i = 0; i < townList.size(); i++) {
            TownDO townDO = townList.get(i);
            if (StringUtils.isBlank(townDO.getStarDoorJson())) {
                continue;
            }
            TownDO.StarDoor starDoor = townDO.getStarDoor();
//            ItemStack itemStack = new ItemStack(starDoor.getType());
            ItemStack itemStack = NBT.itemStackFromNBT(NBT.parseNBT(starDoor.getType()));
            OfflinePlayer leader = null;
            if (StringUtils.isNotBlank(townDO.getLeaderUuid())) {
                leader = PlayerTools.getOfflineOrOnlinePlayerByUuid(townDO.getLeaderUuid());
            }
            ItemMeta itemMeta = itemStack.getItemMeta();
            ArrayList<TextComponent> lore = Lists.newArrayList(
                    Component.text("&f状态: ")
                            .append(
                                    townDO.getTownLevel() == TownLevel.DUST ? Component.text("&c已停用") : Component.text("&a已解锁")
                            ),
                    leader == null ? null : Component.text("&f镇长: &a" + leader.getName()),
                    townDO.getTownLevel() == TownLevel.DUST ? null : Component.text("&e◆ 点击传送")
            );
            String desc = starDoor.getDesc();
            if (StringUtils.isNotBlank(desc)) {
                int length = 16;
                int ceil = (int) Math.ceil(1.0 * desc.length() / length);
                for (int j = 0; j < ceil; ++j) {
                    if (j == ceil - 1) {
                        lore.add(Component.text(desc.substring(j * length)));
                    } else {
                        lore.add(Component.text(desc.substring(j * length, (j + 1) * length)));
                    }
                }
            }
            itemMeta.lore(lore);
            itemMeta.displayName(Component.text(starDoor.getTitle()));
            itemStack.setItemMeta(itemMeta);
            BSBuy bsBuy = bs.getAPI().createBSBuy(BSRewardType.Command, BSPriceType.Nothing,
                    townDO.getTownLevel() == TownLevel.DUST ? null : Collections.singletonList(String.format(tpCommand, player.getName(), starDoor.getPos())),
                    null, null, i, null);
            shop.addShopItem(bsBuy, itemStack, bs.getClassManager());
        }
        // 添加返回按钮
        ItemStack itemStack = new ItemStack(Material.ARROW);
        itemStack.lore(Collections.singletonList(Component.text("&7点击返回主菜单")));
        ItemMeta itemMeta = itemStack.getItemMeta();
        itemMeta.displayName(Component.text("&a◆ 返回_"));
        itemStack.setItemMeta(itemMeta);
        BSBuy bsBuy = bs.getAPI().createBSBuy(BSRewardType.Shop, BSPriceType.Nothing, "menu", null, null, 26, null);
        shop.addShopItem(bsBuy, itemStack, bs.getClassManager());
        bs.getAPI().openShop(player ,shop);
    }
}
